<?php
/*
	申请成为代理商
	如果已申请，但尚未审核通过，则页面显示请耐心等待管理员审核！
	如果已申请，且审核通过，则显示审核通过，并提供按钮前往代理中心
	如果审核不通过，则显示审核不通过原因
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['ischecklogin']=model('jy_weishop/member')->checklogin('','','','',true);
if($_RQ['op']=='main'){
	set_html('title','申请成为单位客户');
	$member=pdo_get('jy_weishop_member',array('mid'=>$_SESSION['uid'],'merchid'=>0,'uniacid'=>$_SESSION['uniacid']));
	//判断是否满足申请分销商条件
	$can_apply=true;
	$return['can_apply']=$can_apply;	
	$return['member']=$member;
	$setting=uni_setting('register');
	$return['dxmb']=$setting['dxmb'];
	$return['post']=model('core/member')->detail($_SESSION['uid'],array('nickname','avatar','realname','mobile','wechat'));
	exi($return);
}
if($_RQ['op']=='apply'){
	$post=get_postdata();
	$member=pdo_get('jy_weishop_member',array('mid'=>$_SESSION['uid'],'merchid'=>0,'uniacid'=>$_SESSION['uniacid']));
	if($member['yuejie_type']==1){
		exi('您已是月结客服，无需再申请！！','error');
	}
	if($member['yuejie_type']==2){
		exi('您已是月结客服的员工，无需再申请！！','error');
	}
	$setting=uni_setting('register');
	if($_SESSION['container']!='wxapp' && $setting['dxmb'] && !check_sms_code($post['smscode'])){
		//exi('手机验证码错误！','error');
	}
	if(!$post['agree']){
		exi('请先阅读协议！','error');
	}
	if(!$post['realname']){
		exi('请输入姓名！','error');
	}
	if(!$post['mobile']){
		exi('请输入联系电话！','error');
	}
	if($this->setting['yuejie_open'] && $this->setting['yuejie_price']>0){
		$status=3;
	}
	$member_update=array(
			'realname'=>trim($post['realname']),
			'mobile'=>trim($post['mobile']),
			'address'=>trim($post['address']),
			'wechat'=>trim($post['wechat']),
	);
	model('core/member')->member_update($_SESSION['uid'],$member_update);
	if($this->setting['yuejie_open'] && $this->setting['yuejie_price']>0){
			$insert=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$_SESSION['uid'],
					'money'=>$this->setting['yuejie_price'],
					'createtime'=>TIMESTAMP,
			);
			pdo_insert('jy_yuejie_viplog',$insert);
			$id=pdo_insertid();
			$params['fee']=$this->setting['yuejie_price'];
			$params['title']='单位客户入驻';
			$params['id']=$id;
			$params['plugin']='jy_yuejie';
			$params['no_type']=array('hdfk');
			$params['back_url']='jy_yuejie/apply/payresult/';
			$payid=cfc('pay')->create_pay_order($params);
			$this->pdo_update('viplog',array('pay_id'=>$payid),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			$res=array(
					'pay_id'=>$payid,
					'order_id'=>$id,
			);
			pdo_update('jy_weishop_member',array('yuejie_type'=>3),array('mid'=>$_SESSION['uid'],'merchid'=>0,'uniacid'=>$_SESSION['uniacid']));
			exi($res);
	}else{
		$this->model('member')->apply_agent($_SESSION['uid']);
	}
	exi('申请成功！请耐心等待管理员审核！','','jy_yuejie/index/');
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result']; 
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','main');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','main');
	}
	$order=pdo_get('jy_yuejie_viplog',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['status']){
		exi('订单已支付，无需重复支付！','error');
	}
	if($order['money']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	pdo_update('jy_yuejie_viplog',array('status'=>1),array('id'=>$order['id'],'uniacid'=>$_SESSION['uniacid']));
	$member=pdo_get('jy_weishop_member',array('mid'=>$order['mid'],'merchid'=>0,'uniacid'=>$_SESSION['uniacid']));
	$member['yuejie_endtime']=$member['yuejie_endtime']?:time();
	$update=array('yuejie_type'=>$return['pluginset']['become_check']?4:1,'yuejie_endtime'=>$member['yuejie_endtime']+365*24*3600);
	if($this->setting['shoplevel_id']){
		$update['level']=$this->setting['shoplevel_id'];	
	}
	pdo_update('jy_weishop_member',$update,array('mid'=>$order['mid'],'merchid'=>0,'uniacid'=>$_SESSION['uniacid']));
	$this->model('member')->checkinlog($order['mid']);
	exi('支付成功','success','jy_yuejie/index/');
}
if($_RQ['op']=='intype0'){
	$this->model('member')->upinlog(0);
	exi('选择成功！');
}